//
//  SecuritySetViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/19.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "SecuritySetViewController.h"
#import "SettingCell.h"
#import "MarginCell.h"
#import "ChangePwdViewController.h"
#import "PhoneNumViewController.h"
#import "LockController.h"
#import "SwitchTableViewCell.h"
#import "RiskEnsureController.h"
#import "SetTradPwdViewController.h"
//手势密码
#import "GestureViewController.h"
#import "GestureVerifyViewController.h"
#import "PCCircleViewConst.h"
#import "CheckoutPwdViewController.h"

static NSString * const SettingCellReuseId = @"SettingCell";
static NSString * const MarginCellReuseId = @"MarginCell";
static NSString * const SwitchTableViewCellReuseId = @"SwitchTableViewCell";

@interface SecuritySetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
/** <#注释#> */
@property (nonatomic,strong) NSMutableArray *titleArr;

@end

@implementation SecuritySetViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];  
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"安全设置";
    _titleArr = @[@[@"手机号",@"修改登录密码",@"修改交易密码"].mutableCopy,@[@"开启手势密码"].mutableCopy].mutableCopy ;
    [self registerCell];
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:SettingCellReuseId bundle:nil] forCellReuseIdentifier:SettingCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:MarginCellReuseId bundle:nil] forCellReuseIdentifier:MarginCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:SwitchTableViewCellReuseId bundle:nil] forCellReuseIdentifier:SwitchTableViewCellReuseId];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titleArr.count + 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return section == 0 ? 1 : [_titleArr[section - 1] count];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    if (indexPath.section == 0) {
        MarginCell *cell = [tableView dequeueReusableCellWithIdentifier:MarginCellReuseId forIndexPath:indexPath]; 
        return cell;
    }else if (indexPath.section == 1) {
        SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellReuseId forIndexPath:indexPath]; 
        cell.titleLb.text = _titleArr[indexPath.section - 1][indexPath.row];
        if (indexPath.row == 0) {
            cell.detailLb.text = [[[NSUserDefaults standardUserDefaults] objectForKey:AppUserPhone] stringByReplacingCharactersInRange:NSMakeRange(3,4) withString:@"****"];
        }
        return cell;
    }
    else{
        if (indexPath.row == 0) {
            SwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SwitchTableViewCellReuseId forIndexPath:indexPath]; 
            cell.titleLb.text = _titleArr[indexPath.section - 1][indexPath.row];
            [cell.switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            return cell;
        }else{
            SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellReuseId forIndexPath:indexPath]; 
            cell.titleLb.text = _titleArr[indexPath.section - 1][indexPath.row];
            return cell;
        }
        
    }
}

- (void)switchAction:(id)sender
{
    UISwitch *switchBtn = (UISwitch *)sender;
    BOOL isButtonOn = [switchBtn isOn];
    if(isButtonOn){
        [_titleArr[1] addObject:@"修改手势密码"];
        NSArray *indexPaths = @[
                                [NSIndexPath indexPathForRow:1 inSection:2],
                                ];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];
        GestureViewController *gestureVc = [[GestureViewController alloc] init];
        gestureVc.type = GestureViewControllerTypeSetting;
        [self.navigationController pushViewController:gestureVc animated:YES];
        NSLog(@"是");
    }else{
        [_titleArr[1] removeObject:@"修改手势密码"];
        NSArray *indexPaths = @[
                                [NSIndexPath indexPathForRow:1 inSection:2],
                                ];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView endUpdates];
        NSLog(@"否");   
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = WB_RGBColor(240, 240, 240);
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        //风险保证金
        RiskEnsureController *VC= [[RiskEnsureController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }else if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            ChangePwdViewController *vc = [[ChangePwdViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 0) {
            PhoneNumViewController *vc = [[PhoneNumViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 2) {
            CheckoutPwdViewController *vc = [[CheckoutPwdViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else if (indexPath.section == 2) {
       if (indexPath.row == 1) {
            //修改手势密码
           GestureVerifyViewController *gestureVerifyVc = [[GestureVerifyViewController alloc] init];
           gestureVerifyVc.isToSetNewGesture = YES;
           [self.navigationController pushViewController:gestureVerifyVc animated:YES];
        }
    }
}

#pragma mark - 懒加载  Lazy Load
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabbarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = WB_RGBColor(240, 240, 240);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.sectionFooterHeight = 0.f;
        _tableView.sectionHeaderHeight = 10.f;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
